<?php

class Login extends ExtendedController {

    private $captchaExpiration = 1800;

    function __construct() {
        parent::__construct();
    }

    function index() {
        if (in_logged($this->session->userdata('logged_in'), '')) {
            redirect($this->session->userdata['user']['controller']);
        } else {
            $tryed = ($this->session->userdata('tryed') != '') ? $this->session->userdata('tryed') : 0;

            if ($tryed >= 3) {
                $this->load->plugin('captcha');

                $captchaValues = array(
                    'word' => rand(10000, 99999),
                    'img_path' => './webroot/graphics/captcha/',
                    'img_url' => base_url() . 'webroot/graphics/captcha/',
                    'font_path' => 'system/fonts/texb.ttf',
                    'img_width' => 154,
                    'img_height' => 30,
                    'expiration' => $this->captchaExpiration
                );

                $captcha = create_captcha($captchaValues);

                $captchaObject = new MCaptcha();
                $captchaObject->captcha_time = $captcha['time'];
                $captchaObject->ip_address = $this->input->ip_address();
                $captchaObject->word = $captcha['word'];
                $captchaObject->save();

                $captchaObject->clear();

                $captchaObject->where('captcha_time <', (time() - $this->captchaExpiration))->get();
                $captchaObject->delete_all();
            }

            $viewValues = array(
                'viewHeader' => array(
                    'title' => 'Login'
                ),
                'viewLogin' => array(
                    'captcha' => ((isset($captcha) ? $captcha['image'] : null))
                ),
                'viewFooter' => array(
                )
            );

            $this->loadViews(array('viewHeader', 'viewLogin', 'viewFooter'), $viewValues);
        }
    }

    function validate() {
        if ($this->input->post('name') == '') {
            //redirect('login');
            echo 'drgrtg';
        }

        $error = array();

        $tryed = ($this->session->userdata('tryed') != '') ? $this->session->userdata('tryed') : 0;

        $captchaObject = new MCaptcha();
        $captchaObject->where('captcha_time <', (time() - $this->captchaExpiration))->get();
        $captchaObject->delete_all();

        $captchaObject->clear();

        $captchaOK = true;

        if ($tryed >= 3) {
            $captchaObject->get_where(array(
                'word' => $this->input->post('captcha'),
                'ip_address' => $this->input->ip_address(),
                'captcha_time >=' => (time() - $this->captchaExpiration)
                )
            );
            $captchaOK = ($captchaObject->result_count() == 0) ? false : true;
            $captchaObject->clear();;
        }

        if(!$captchaOK) {
            $error[] = 'Die eingegebene Zeichenfolge ist falsch!';
        }

        $user = new MUser();
        $user->get_by_name($this->input->post('name'));

        if(!$user->exists() || $user->password != encrypt_pw($this->input->post('password'), $user->salt)) {
            $error[] = 'Passwort und/oder Name falsch!';
        }

//        $form_rand = 'pw_' . strtolower($_POST['form_rand']);

        if (count($error) > 0) {
            $sessdata = array(
                'tryed' => ($tryed + 1)
            );
            $this->session->set_userdata($sessdata);

            $this->index();
        } else {
            $sessdata = array(
                'user' => array(
                    'id' => $user->id,
                    'controller' => $user->controller
                ),
                'logged_in' => true,
                'tryed' => 0,
                'last_login' => $user->last_login
            );
            $this->session->set_userdata($sessdata);

            $user->last_login = time();
            $user->save();

            redirect($user->controller);
        }

        var_dump($error);
    }

    function logout() {
        in_logged($this->session->userdata('logged_in'));

        $sessdata = array(
            'user' => '',
            'logged_in' => '',
            'last_login' => ''
        );
        $this->session->unset_userdata($sessdata);

        redirect('login');
    }

    function test() {
        $user = new MUSer();
        $user->get_by_name('flo');
        $user->password = encrypt_pw('passwort', $user->salt);
        $user->save();
    }
}